package com.example.shop.controller;

import com.example.shop.domain.Goods;
import com.example.shop.domain.Result;
import com.example.shop.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @projectName: travel
 * @package: com.example.travel.controller
 * @className: OutlayController
 * @author: Liqian
 * @description: TODO
 * @date: 2024/4/12 15:26
 * @version: 1.0
 */
@RestController
@RequestMapping("/goods")
@Validated
@Slf4j
@Tag(name = "出租物品发布", description = "这是描述")

public class GoodsController {


    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    @Operation(summary = "查询本人列表")
    public Result list() {
        return Result.success(goodsService.selectList());
    }

    @GetMapping("/selectById")
    @Operation(summary = "通过id查询")
    public Result selectById(@RequestParam Integer id) {
        Goods goods = new Goods();
        goods.setId(id);
        return Result.success(goodsService.selectById(goods));
    }

    @GetMapping("/selectOne")
    @Operation(summary = "通过id查询")
    public Result selectOne(@RequestParam Integer id) {
        Goods goods = new Goods();
        goods.setId(id);
        return Result.success(goodsService.selectOne(goods));
    }

    @PostMapping("/add")
    @Operation(summary = "添加")
    public Result add(@RequestBody @Validated Goods goods) {
        goods.setStatus(0);
        goods.setAgreementId(0);
        goodsService.add(goods);
        return Result.success();
    }

    @PutMapping("/update")
    @Operation(summary = "更新")
    public Result update(@RequestBody @Validated Goods goods) {
        System.out.println("更新");
        List<Goods> list = goodsService.selectById(goods);
        if (list.isEmpty()) {
            return Result.error("此账户中不存在该记录");
        }
        goodsService.update(goods);
        return Result.success();
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除")

    public Result delete(@RequestBody Goods goods) {
//        安全校验
        List<Goods> list = goodsService.selectById(goods);
        if (list.isEmpty()) {
            return Result.error("此账户中不存在该记录");
        }
        goodsService.delete(goods);
        return Result.success();
    }


}
